package com.lakeworks.processor;

import com.lakeworks.common.domain.channel.ticket.TicketAdvance;
import com.lakeworks.common.domain.ticket.TktTicket;
import com.lakeworks.common.enums.ticket.*;
import com.lakeworks.common.utils.StringUtils;
import com.lakeworks.decision.AdvanceTicketDecision;
import com.lakeworks.decision.BackTicketStateDecision;
import com.lakeworks.decision.PushTicketDecision;
import com.lakeworks.decision.base.Decision;
import com.lakeworks.domain.TktProcess;
import com.lakeworks.event.AdvanceTicketEvent;
import com.lakeworks.event.BackTicketEvent;
import com.lakeworks.mapper.TktRouteMapper;
import com.lakeworks.mapper.TktTicketMapper;
import com.lakeworks.processor.base.AbstractTicketProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AdvanceTicketProcessor extends AbstractTicketProcessor<AdvanceTicketEvent> {


    @Override
    public TktProcess apply(
            AdvanceTicketEvent event
    ) {
        var process = super.apply(event);
        TicketAdvance advance = event.getTicketAdvance();
        TktTicket ticket = event.getTicket();
        if(event.getTktRoute().getNodeId() != 9999L){
            ticket.setNodeId(event.getTktRoute().getNodeId());
        }
        if(advance.getEventType().equals(RouteKey.FACTORYHANDLE.name())
                && ticket.getType().equals(FlowType.QUANTITY_MISMATCH.name())){
            ticket.setNodeId(NodeType.QUANTITY_MISMATCH_ONE.getValue());
        }
        process.setTicket(ticket);
        process.setTicketId(ticket.getId());
        process.setComment("平台:【"+RouteKey.valueOf(advance.getEventType()).getName()+"】");
        if(StringUtils.isNotBlank(advance.getUpdateType())){
            process.setComment("平台:【"+advance.getUpdateType()+"】");
        }
        process.setRemark(ticket.getRemark());
        if(RouteKey.ExFACTORY.name().equals(advance.getEventType())){
            process.setFlag(2);
        }
        List<Decision> list = new ArrayList<>();
        list.add(new AdvanceTicketDecision(advance,ticket));
        process.setDecisions(list);
        return process;
    }


}